//
//  YQRadioListViewController.m
//  EPA--1
//
//  Created by lanou3g on 16/4/23.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "YQRadioListViewController.h"
#import "YQRadioModel.h"
#import "YQScrollModel.h"
#import "YQRadioListCollectionViewCell.h"
#import "YQCycleView.h"
#import "YQRadioTableViewController.h"
@interface YQRadioListViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) NSMutableArray *allListArray;
@property (nonatomic, strong) NSMutableArray *hotListArray;
@property (nonatomic, strong) NSMutableArray *imgNameArray;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) YQCycleView *cycleView;



@end

@implementation YQRadioListViewController
- (void)dealloc
{
    [self.collectionView removeObserver:[self.collectionView defaultFooter] forKeyPath:@"contentOffset"];
    [self.collectionView removeObserver:[self.collectionView defaultFooter] forKeyPath:@"contentSize"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.allListArray = [NSMutableArray new];
    self.hotListArray = [NSMutableArray new];
    self.imgNameArray = [NSMutableArray new];
    
    [self loadData];
    NSLog(@"%@",self.allListArray);
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
//    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds  collectionViewLayout:layout];
    layout.itemSize = CGSizeMake(108, 180);
//    collectionView.backgroundColor = [UIColor grayColor];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.headerReferenceSize = CGSizeMake(100, 200);
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    [self.collectionView registerNib:[UINib nibWithNibName:@"YQRadioListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"list"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [self addLable];
    [self creatScrollView];
    [self refreshCollectionView];
           }
- (void)refreshCollectionView
{
    __weak typeof(self)weakSelf = self;
    [self.collectionView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger count = weakSelf.hotListArray.count;
        [weakSelf loadDataRefreshWithStart:count];
    }];

}
- (void)creatScrollView
{
    self.cycleView = [[YQCycleView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150) interVal:2];
    self.cycleView.imgNameArray = @[@"1",@"2",@"3",@"4",@"5"].mutableCopy;
}
-  (void)addLable
{
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 170, 100, 20)];
    label.tag = 1000;
    [self.view addSubview:label];
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(20, 170, 100, 20)];
    label2.tag = 1001;
   [self.view addSubview:label2];

}
- (void)loadDataRefreshWithStart:(NSInteger)start
{
    [YQNetWorkRequestManager requestURL:[NSURL URLWithString:RADIOMLISTORE_URL] method:YQ_POST params:@{@"auth":@"XZU7RH7m1861DC8Z8H8HvkTJxQMGoPLGO9zo4XDA0cWP22NdFSh9d7fo",@"client":@1,@"deviceid":@"6D4DD967-5EB2-40E2-A202-37E64F3BEA31",@"limit":@9,@"start":@(start),@"version":@"3.0.6"} result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
             NSLog(@"%@",dict);
            NSDictionary *dataDict = dict[@"data"];
            for (NSDictionary *dic  in dataDict[@"list"]) {
                YQRadioModel *model = [YQRadioModel new];
                [model setValuesForKeysWithDictionary:dic];
                [self.hotListArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
                [[self.collectionView defaultFooter] endRefreshing];
            });
        }
    }];
}
- (void)loadData
{
    [YQNetWorkRequestManager requestURL:[NSURL URLWithString:RADIOLIST_URL] method:YQ_POST params:@{@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"",@"version":@"3.0.2"} result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//             NSLog(@"%@",dict);
            NSDictionary *dataDict = dict[@"data"];
            for (NSDictionary *dic in dataDict[@"alllist"]) {
                YQRadioModel *model = [YQRadioModel new];
                [model setValuesForKeysWithDictionary:dic];
                [self.allListArray addObject:model];
            }
            for (NSDictionary *dic in dataDict[@"hotlist"]) {
                YQRadioModel *model = [YQRadioModel new];
                [model setValuesForKeysWithDictionary:dic];
                [self.hotListArray addObject:model];
            }
            for (NSDictionary *dic in dataDict[@"carousel"]) {
                YQScrollModel *model = [YQScrollModel new];
                [model setValuesForKeysWithDictionary:dic];
                [self.imgNameArray addObject:model];
            }
#warning --有问题呀?????????????
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
//                NSMutableArray *array = [NSMutableArray array];
//                for (YQScrollModel *model in self.imgNameArray) {
//                    [array addObject:model.img];
//                }
//                self.cycleView.urlNameArray = array;

            });
            
        }
    }];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.allListArray.count;
    }
    if (section == 1) {
        
        return self.hotListArray.count;
        
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YQRadioListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"list" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.model = self.allListArray[indexPath.row];
        return cell;
    }
    if (indexPath.section == 1) {
        cell.model = self.hotListArray[indexPath.row];
        return cell;
    }
    return nil;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        if (indexPath.section == 0) {
            UILabel *label = [self.view viewWithTag:1000];
            label.text = @"ALL~LIST";
            [headerView addSubview:label];
            [headerView addSubview:self.cycleView];
            UILabel *label2 = [self.view viewWithTag:1001];
            [headerView addSubview:label2];
            label2.text = @"";
            return headerView;
        }else{
            UILabel *label2 = [self.view viewWithTag:1001];
            [headerView addSubview:label2];
            label2.text = @"HOT~LIST";
            UILabel *label = [self.view viewWithTag:1000];
            label.text = @"";
            [headerView addSubview:label];
            
            return headerView;
        }
        }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YQRadioTableViewController *radioVC = [YQRadioTableViewController new];
    if (indexPath.section == 0) {
        YQRadioModel *model = self.allListArray[indexPath.row];
        radioVC.radioid = model.radioid;
//      radioVC.deviceid = model.
    }
    if (indexPath.section == 1) {
        YQRadioModel *model = self.hotListArray[indexPath.row];
        radioVC.radioid = model.radioid;
    }
    
    
    [self.navigationController pushViewController:radioVC animated:YES];
}
//- (UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout
//{
//    
//}
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
//    if (section == 0) {
//        
//        return CGSizeMake(100, 200);
//    }else
//    {
//        return CGSizeMake(100, 60);
//    }
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
